package com.tfswx.fljdmxyy.service.impl;

import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.ObjectUtil;
import com.newbie.core.exception.BusinessException;
import com.tfswx.fljdmxyy.constant.SystemConstant;
import com.tfswx.fljdmxyy.dao.RwXtPzMapper;
import com.tfswx.fljdmxyy.dao.RwYxRzMapper;
import com.tfswx.fljdmxyy.dto.rwpz.*;
import com.tfswx.fljdmxyy.enums.BusinessCodeEnum;
import com.tfswx.fljdmxyy.model.RwXtPz;
import com.tfswx.fljdmxyy.query.rwpz.RwpzQueryParam;
import com.tfswx.fljdmxyy.query.rwpz.RwpzQueryResult;
import com.tfswx.fljdmxyy.query.rwrz.RwrzQueryParam;
import com.tfswx.fljdmxyy.query.rwrz.RwrzQueryResult;
import com.tfswx.fljdmxyy.service.RwglService;
import com.tfswx.fljdmxyy.taskContext.TaskConfigurer;
import com.tfswx.fljdmxyy.utils.ConverterUtils;
import com.tfswx.fljdmxyy.utils.UuidUtils;
import com.tfswx.newbie.boot.starter.page.PageHelper;
import com.tfswx.newbie.boot.starter.page.PageParam;
import com.tfswx.newbie.boot.starter.page.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.support.CronExpression;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 任务管理实现类
 *
 * @Author 陈文佳
 * @Date 2023/03/24
 */
@Slf4j
@Service
public class RwglServiceImpl implements RwglService {

    @Resource
    private RwYxRzMapper rwYxRzMapper;

    @Resource
    private RwXtPzMapper rwXtPzMapper;

    @Resource
    private TaskConfigurer taskConfigurer;

    @Resource
    private TaskServiceImpl taskContextServiceImpl;

    @Override
    public RwpzAddOutputDTO addRwpz(RwpzAddInputDTO inputDTO) {
        // 1.校验cron表达式是否正确
        if (!CronExpression.isValidExpression(inputDTO.getRwbds())) {
            throw new BusinessException(BusinessCodeEnum.TJSJSB.getCode(), "cron表达式格式错误");
        }

        // 2.设置任务配置参数
        RwXtPz entity = ConverterUtils.convert(inputDTO, RwXtPz.class);
        Date date = new Date();
        entity.setRwbh(UuidUtils.uuid32());
        entity.setCjsj(date);
        entity.setZhxgsj(date);
        entity.setSfzt(ObjectUtil.defaultIfEmpty(inputDTO.getSfzt(),SystemConstant.N));
        entity.setSfqy(ObjectUtil.defaultIfEmpty(inputDTO.getSfqy(),SystemConstant.Y));
        entity.setSfsc(SystemConstant.N);

        // 3.新增任务配置
        int result = rwXtPzMapper.insertRwpz(entity);
        if (result < 1) {
            throw new BusinessException(BusinessCodeEnum.TJSJSB);
        }

        // 4.重新加载任务配置
        refreshTask();

        // 5.返回结果
        return ConverterUtils.convert(entity, RwpzAddOutputDTO.class);
    }

    @Override
    public Boolean updateRwpz(RwpzUpdateInputDTO inputDTO) {
        // 1.校验cron表达式是否正确
        if (!CronExpression.isValidExpression(inputDTO.getRwbds())) {
            throw new BusinessException(BusinessCodeEnum.XGSB.getCode(), "cron表达式格式错误");
        }

        // 2.判断当前任务是否启用，未启用则暂停任务
        RwXtPz entity = ConverterUtils.convert(inputDTO, RwXtPz.class);
        if (CharSequenceUtil.equals(entity.getSfqy(), SystemConstant.N)) {
            entity.setSfzt(SystemConstant.Y);
        }
        entity.setZhxgsj(new Date());

        // 3.修改任务配置
        int result = rwXtPzMapper.updateRwpz(entity);
        if (result < 1) {
            throw new BusinessException(BusinessCodeEnum.XGSJSB);
        }

        // 4.重新加载任务列表
        refreshTask();

        return Boolean.TRUE;
    }

    @Override
    public RwpzGetOutputDTO getRwpz(RwpzGetInputDTO inputDTO) {
        RwXtPz entity = ConverterUtils.convert(inputDTO, RwXtPz.class);
        return ConverterUtils.convert(rwXtPzMapper.getRwpz(entity), RwpzGetOutputDTO.class);
    }

    @Override
    public Boolean deleteRwpz(RwpzDeleteInputDTO inputDTO) {
        RwXtPz entity = ConverterUtils.convert(inputDTO, RwXtPz.class);
        // 1.删除任务配置
        int result = rwXtPzMapper.deleteRwpz(entity);
        if (result < 1) {
            throw new BusinessException(BusinessCodeEnum.SCSJSB);
        }

        // 2.重新加载任务列表
        refreshTask();

        return Boolean.TRUE;
    }

    /**
     * 重新加载任务列表
     */
    private void refreshTask() {
        RwpzQueryParam param = new RwpzQueryParam();
        param.setSfqy(SystemConstant.Y);
        param.setSfzt(SystemConstant.N);
        List<RwXtPz> list = rwXtPzMapper.queryRwpz(param);
        taskConfigurer.refreshTask(list);
    }

    @Override
    public PageResult<RwpzQueryResult> queryRwpz(PageParam<RwpzQueryParam> param) {
        return PageHelper.doSelectPage(param, () -> rwXtPzMapper.queryRwpzlb(param.getFilter()));
    }

    @Override
    public Boolean executeRw(RwpzGetInputDTO inputDTO) {
        taskContextServiceImpl.execute(rwXtPzMapper.getRwpz(ConverterUtils.convert(inputDTO, RwXtPz.class)));
        return Boolean.TRUE;
    }

    @Override
    public Boolean clearRwrz() {
        return rwYxRzMapper.clearRwrz() > 0;
    }

    @Override
    public PageResult<RwrzQueryResult> queryRwrz(PageParam<RwrzQueryParam> param) {
        return PageHelper.doSelectPage(param, () -> rwYxRzMapper.queryRwrz(param.getFilter()));
    }
}
